package com.zyh.cxf;


import com.zyh.dao.KeyMapper;
import com.zyh.util.RandomNumber;
import com.zyh.util.safeutil.DESedeUtil;
import com.zyh.util.safeutil.SM2Utils;
import com.zyh.util.safeutil.Util;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Map;

@Controller
public class MainClientCxfZYH {



    @Test
    public void test01(){

        try {

            JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();

            String linuxWebURL = "http://10.229.1.149:8091/ssm/webservices/getAccountBal";//

            String localWebURL = "http://localhost:8080/ssm/webservices/getAccountBal";

            jaxWsProxyFactoryBean.setAddress(localWebURL);

            jaxWsProxyFactoryBean.setServiceClass(GetAccountBalance.class);

            GetAccountBalance getAccountBalance = (GetAccountBalance) jaxWsProxyFactoryBean.create();

            String xmlData = "";

            Document doc = DocumentHelper.createDocument();
            Element Message = doc.addElement("Message");
            Element Head = Message.addElement("Head");
            Element Body = Message.addElement("Body");


            Element serialNo = Head.addElement("serialNo");
            Element reqTime = Head.addElement("reqTime");

            serialNo.setText("1");
            reqTime.setText("20201217");

            Element PayerAcNO = Body.addElement("PayerAcNo");
            Element PayerCurrency = Body.addElement("PayerCurrency");

            PayerAcNO.setText("82010000000459867");// 82010000000369927  131657-error: 没有找到符合条件的记录！
            PayerCurrency.setText("01");

            String inencryptKey="041C42F62CD0D3BD7236BCF1ADDD480BA86A7406F79ECFD3F2A52C49F03A57D78F6384399D33BF502D2744EC3A2DFBA54B4E9B38EC5FBF05D808B458B19DB2C1B67E49DB065AF74DD01FE4BC7FDCF4C4DE78F3548AFB77898EA6E47A2E08248E75C131E88A47CF74B28CE655E76005684EA33EBB28C99A7561469F461B079F7327";

            String privateKey="421DF5A971BA443F561F9642174B24210EAFCC4304FC9F86399972F3B5132123";

            //第一步密钥解密
            System.err.println("密钥sm2私钥解密：");
            String plainText = null;
            try {
                plainText = new String(SM2Utils.decrypt(Util.hexToByte(privateKey), Util.hexToByte(inencryptKey)));
            } catch (IOException e) {
                e.printStackTrace();
            }
            System.out.println("密钥明文：" + plainText);



            String inencryptStr = null;
            try {
                inencryptStr = DESedeUtil.encrypt(doc.asXML(), plainText);
                String data = getAccountBalance.getABalance(inencryptStr);
                //data = "OyBBzjRiQ++xD9dqGa2subhRmh52I8RVf26mkSBQrWmOqdeOxP6OGiBwgfk3T7pWoiNRTh8qLaVg1kFspJNmLVxk1v1mly/DvBDC0OYPedP4+lmBrdZ6SE2ztaFUnuaiBQ0ldcX/6pJMh6WThAtA469zli2g3f8vnL1linQcqwTN2tDQBh+/gcnLCJP+Yby6TXi1meceBpqe0m+uCUs2OFg/t0y0ALlmdhyMc1Qy06L+eVqc2litdciI1chobn3q3mV/ueknhmJ/tbg1oxuDNane2dv/HQsc/3zXtmh9C3u4Jx9sNnkoKwDOOX78WH0ah0iiqHR/4bx0BM0KQNVPj5s6V0jfJpVegtai2773d3G1DU88VpeiKLTMPVUzziHl79JzH5yyUbdYP7dMtAC5ZifAgKpntXHNGhaBAqtWyB4/C7cl7kKuY5wdd3uZq9xKt++VJ4LDPa+gZ7J9LAwcdgONhAwwCsoFALQy+3bCP/6Azx1n2qG60ASTjjoVRkBv1+H0hXzr2Lqei6w8Xu4YwjoZtsxOX/c0ucZJsb+BfbTB2TC1aTil37Ee0t5nt7YuoGeyfSwMHHZKXcq+leOjz2VW5Q2QYT+xNyQmVaiuWbF7XDE+tmaQda5tytCbzZnee8M+QXn0U96sZ43OQubuHHyKbJsu09+K77TqZa85U2oznmae/gvVh2ZuZV1IdpVzFsOb1yua4q+oKxo4Ky5my6n61pn7CNXOe8S4NwJ4DS5SH6L2evPvjSTumQ8nzZ44JuEYrwOX3TM= ";
                System.out.println(DESedeUtil.decrypt(data, plainText));
            } catch (Exception e) {
                e.printStackTrace();
            }

            //加密




        }catch (Exception e){

            System.out.println(e.getLocalizedMessage());
        }

    }
    @Test
    public void test23() {

        JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();

        String linuxWebURL = "http://10.229.1.149:8091/ssm/webservices/getAccountgetTrandBills";

        String localWebURL = "http://localhost:8080/ssm/webservices/getAccountgetTrandBills";

        jaxWsProxyFactoryBean.setAddress(localWebURL);

        jaxWsProxyFactoryBean.setServiceClass(GetAccountBalanceDes.class);

        GetAccountBalanceDes getAccountBalanceDes = (GetAccountBalanceDes) jaxWsProxyFactoryBean.create();

        Document doc = DocumentHelper.createDocument();
        Element Message = doc.addElement("Message");
        Element Head = Message.addElement("Head");
        Element Body = Message.addElement("Body");


        Element serialNo = Head.addElement("serialNo");
        Element reqTime = Head.addElement("reqTime");
        Element bsnCode = Head.addElement("bsnCode");
        serialNo.setText("40220201216195506");
        reqTime.setText("20201216195506");
        bsnCode.setText("QRCB002");
        Element CifNo = Head.addElement("CifNo");
        CifNo.setText("");

        Element PayerAcNO = Body.addElement("PayerAcNo");
        Element BeginDate = Body.addElement("BeginDate");
        Element EndDate = Body.addElement("EndDate");
        Element PayerCurrency = Body.addElement("PayerCurrency");

        PayerAcNO.setText("82010000000303239");//  82010000000303239
        BeginDate.setText("20010109");
        EndDate.setText("20201231");
        PayerCurrency.setText("01");


        String inencryptKey="041C42F62CD0D3BD7236BCF1ADDD480BA86A7406F79ECFD3F2A52C49F03A57D78F6384399D33BF502D2744EC3A2DFBA54B4E9B38EC5FBF05D808B458B19DB2C1B67E49DB065AF74DD01FE4BC7FDCF4C4DE78F3548AFB77898EA6E47A2E08248E75C131E88A47CF74B28CE655E76005684EA33EBB28C99A7561469F461B079F7327";

        String privateKey="421DF5A971BA443F561F9642174B24210EAFCC4304FC9F86399972F3B5132123";

        //第一步密钥解密
        System.err.println("密钥sm2私钥解密：");
        String plainText = null;
        try {
            plainText = new String(SM2Utils.decrypt(Util.hexToByte(privateKey), Util.hexToByte(inencryptKey)));
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("密钥明文：" + plainText);



        String inencryptStr = null;
        try {
            inencryptStr = DESedeUtil.encrypt(doc.asXML(), plainText);
            String data = getAccountBalanceDes.getABalanceDes(inencryptStr);
            //data = "OyBBzjRiQ++xD9dqGa2subhRmh52I8RVf26mkSBQrWmOqdeOxP6OGiBwgfk3T7pWoiNRTh8qLaVg1kFspJNmLVxk1v1mly/DvBDC0OYPedP4+lmBrdZ6SE2ztaFUnuaiBQ0ldcX/6pJMh6WThAtA469zli2g3f8vnL1linQcqwTN2tDQBh+/gcnLCJP+Yby6TXi1meceBpqe0m+uCUs2OFg/t0y0ALlmdhyMc1Qy06L+eVqc2litdciI1chobn3q3mV/ueknhmJ/tbg1oxuDNane2dv/HQsc/3zXtmh9C3u4Jx9sNnkoKwDOOX78WH0ah0iiqHR/4bx0BM0KQNVPj5s6V0jfJpVegtai2773d3G1DU88VpeiKLTMPVUzziHl79JzH5yyUbdYP7dMtAC5ZifAgKpntXHNGhaBAqtWyB4/C7cl7kKuY5wdd3uZq9xKt++VJ4LDPa+gZ7J9LAwcdgONhAwwCsoFALQy+3bCP/6Azx1n2qG60ASTjjoVRkBv1+H0hXzr2Lqei6w8Xu4YwjoZtsxOX/c0ucZJsb+BfbTB2TC1aTil37Ee0t5nt7YuoGeyfSwMHHZKXcq+leOjz2VW5Q2QYT+xNyQmVaiuWbF7XDE+tmaQda5tytCbzZnee8M+QXn0U96sZ43OQubuHHyKbJsu09+K77TqZa85U2oznmae/gvVh2ZuZV1IdpVzFsOb1yua4q+oKxo4Ky5my6n61pn7CNXOe8S4NwJ4DS5SH6L2evPvjSTumQ8nzZ44JuEYrwOX3TM= ";
            System.out.println(DESedeUtil.decrypt(data, plainText));
        } catch (Exception e) {
            e.printStackTrace();
        }


    }






}
